﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_LivestockType
    {
        public bool CreateOneLivestockType(LivestockType_Info objLivestockType_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockType_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockTypeID", objLivestockType_Info.LivestockTypeID));
            command.Parameters.Add(new SqlParameter(@"LivestockTypeName", objLivestockType_Info.LivestockTypeName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneLivestockType(LivestockType_Info objLivestockType_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockType_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@LivestockTypeID", objLivestockType_Info.LivestockTypeID));
            command.Parameters.Add(new SqlParameter("@LivestockTypeName", objLivestockType_Info.LivestockTypeName));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneLivestockType(int LivestockTypeID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockType_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockTypeID", LivestockTypeID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllLivestockType()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockType_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public LivestockType_Info GetOneLivestockType(int LivestockTypeID)
        {
            LivestockType_Info objLivestockType_Info = new LivestockType_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("LivestockType_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockTypeID", LivestockTypeID));
            try
            {
                objConnection.Open();
                SqlDataReader ordLivestockType = command.ExecuteReader(); //OjectRecordDatareader
                ordLivestockType.Read();
                if (ordLivestockType.HasRows)
                {
                    if (ordLivestockType["LivestockTypeID"] != DBNull.Value)
                    {
                        objLivestockType_Info.LivestockTypeID = Convert.ToInt16(ordLivestockType["LivestockTypeID"]);
                    }
                    if (ordLivestockType["LivestockTypeName"] != DBNull.Value)
                    {
                        objLivestockType_Info.LivestockTypeName = Convert.ToString(ordLivestockType["LivestockTypeName"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objLivestockType_Info;
        }
    }
}
